﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Logic;
using Entities;

namespace Presentation
{
	public partial class MovieInfo : Form
	{
		private Movie movie;

		public Movie Movie
		{
			get { return movie; }
			set { movie = value; }
		}

		public MovieInfo()
		{
			InitializeComponent();
		}

		public MovieInfo(Movie entity)
		{
			movie = entity;
			InitializeComponent();
		}

		private void MovieInfo_Load(object sender, EventArgs e)
		{
			Text += ": "+movie.Title;
			treeViewSections.SelectedNode = treeViewSections.Nodes[0];
			infoBasic.Initialize(movie);
			infoPoster.Initialize(movie);
			infoComments.Initialize(movie);
			infoTags.Initialize(movie);
			infoLists.Initialize(movie);
		}

		private void treeViewSections_AfterSelect(object sender, TreeViewEventArgs e)
		{
			switch (treeViewSections.SelectedNode.Name)
			{
				default:
				case "nodeInfo":
					infoBasic.BringToFront();
					break;
				case "nodePoster":
					infoPoster.BringToFront();
					break;
				case "nodeComments":
					infoComments.BringToFront();
					break;
				case "nodeTags":
					infoTags.BringToFront();
					break;
				case "nodeLists":
					infoLists.BringToFront();
					break;
			}
		}

		private void linkLabelAdd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (openFileDialogAddAPart.ShowDialog() == DialogResult.OK && openFileDialogAddAPart.FileNames.Length > 0)
			{
				List<string> filenames = new List<string>();

				filenames.Add(movie.VideoInfo.FileName);
				filenames.AddRange(openFileDialogAddAPart.FileNames);

				movie.VideoInfo.FileName = String.Join("|", filenames.ToArray());

				infoBasic.Initialize(movie);
			}
		}

		private void buttonAccept_Click(object sender, EventArgs e)
		{
			movie = infoBasic.GetMovie();
			movie.Comments = infoComments.GetComments();
			movie.Tags = infoTags.GetTags();

			MovieBL mbl = new MovieBL();
			mbl.Update(movie);
			mbl.UpdateTags(movie);
            mbl.UpdateGenres(movie);

			if (movie.Poster != infoPoster.GetPoster())
			{
				mbl.ChangePoster(movie, infoPoster.GetPoster());
			}

			DialogResult = DialogResult.OK;
		}
	}
}